import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Servlet",value = "/Servlet")
    public class login extends HttpServlet {
    public login() {
        super();
    }

public void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request,response);
        }
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
        request.getParameter("username");//请求读取表单中的值
        request.getParameter("password");
        request.getSession().setAttribute("user", request.getParameter("username"));//将user放在Attribute中
        Users user=new Users();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        System.out.println(request.getParameter("username"));
        System.out.println(request.getParameter("password"));
    try {
        if (user.validate())//验证
        {
            request.getRequestDispatcher("welcome.jsp").forward(request, response); //校验用户名密码正确，跳转到welcome.jsp
        }//getRequestDispatcher().forward()请求转发即调转到页面
        else {
            request.getRequestDispatcher("index.jsp").forward(request, response); //校验用户名密码不正确，跳转到index.jsp
        }

    } catch (ServletException | IOException e) {
        e.printStackTrace();
    }
}
}
